<!doctype html>
<html>
<head>
    <meta http-equiv="content-type" content="text/html; charset=UTF-8" />
    <title>运维功能——定时sql任务新增/查看页面</title>
    <script src="/webjars/miniui/boot.js" type="text/javascript"></script>
    <link href="/static/css/resource.css" rel="stylesheet" type="text/css" />
    <script src="/static/js/common/common.js" type="text/javascript"></script>
    <link href="/webjars/miniui/css/demo.css" rel="stylesheet" type="text/css" />
    <link href="/static/css/default.css" rel="stylesheet" type="text/css" />
</head>
<body>
<form id="form1" method="post">
    <input name="id" id="id" class="mini-hidden" style="width: 100%;" readonly/>
    <!-- 用户 编辑框START-->
    <div id="editform" class="form" style="padding:20px;">
        <table class="form-table" align="center">
            <tr>
                <td style="width: 100px;text-align: right"><font color="#dc143c">*</font>SQL名称:</td>
                <td>
                    <input name="remark" id="remark" class="mini-textbox" style="width: 450px" required="true" />
                </td>
            </tr>
            <tr>
                <td style="width: 100px;text-align: right"><font color="#dc143c">*</font>SQL内容:</td>
                <td>
                    <input class="mini-textarea" required="true" style="width: 450px;height:200px" emptyText="只支持增、删、改三种SQL,需要限定范围条件，不可带*，需要有where条件，暂只支持单条语句" id="content" name="content"/>
                </td>
            </tr>
            <tr>
                <td style="width: 100px;text-align: right"><font color="#dc143c">*</font>SQL是否执行:</td>
                <td>
                    <input name="type" id="type" class="mini-radiobuttonlist"
                           textField="name" valueField="id" required="true" />
                </td>
            </tr>
            <tr>
                <td style="width: 100px;text-align: right"><font color="#dc143c">*</font>作用省份:</td>
                <td>
                    <input name="province" id="province" class="mini-combobox" style="width: 450px"
                           textField="codeName" valueField="codeId" emptyText="请选择..."
                           required="true" multiSelect="true" onvaluechanged="provinceCheck()"  />
                </td>
            </tr>
            <#if flag==2>
                <tr id="carryResultId" style="display:none">
                    <td style="width: 100px;text-align: right"><font color="#dc143c">*</font>未完成省份:</td>
                    <td>
                        <input name="carryResult" id="carryResult" class="mini-combobox" style="width: 450px"
                               textField="codeName" valueField="codeId" emptyText="已完成" required="true" multiSelect="true"  />
                    </td>
                </tr>
                <tr>
                    <td style="width: 100px;text-align: right"><font color="#dc143c">*</font>创建IP:</td>
                    <td>
                        <input name="createUser" id="createUser" class="mini-textbox" style="width: 200px" required="true" />
                    </td>
                </tr>
                <tr>
                    <td style="width: 100px;text-align: right"><font color="#dc143c">*</font>创建时间:</td>
                    <td>
                        <input name="createTime" id="createTime" class="mini-textbox" style="width: 200px" required="true" />
                    </td>
                </tr>
            </#if>
        </table>
    </div>
    <#if flag!=2>
        <div style="padding:2px;margin: 0 auto;text-align: center">
            <a class="mini-button" iconCls="fa-save" plain="true" onclick="click()">保存</a>
            <a class="mini-button" iconCls="fa-close" plain="true" onclick="onCancel()">取消</a>
        </div>
    </#if>
</form>

<script type="text/javascript">
    mini.parse();
    var form = new mini.Form("form1");
    var flag = ${flag!}
    var sqlType;

    //标准方法接口定义
    function SetData(data) {
        var o = mini.decode(data);
        form.setData(o);
        mini.get("createTime").setValue(mini.formatDate(data.createTime,'yyyy-MM-dd HH:mm:ss'));
        form.setChanged(false);
    }

    $(function () {
        getProvince();
        if(flag===2){
            //查看
            form.setEnabled(false);
            mini.get("type").setData([{ id: 0, name: '未启用' },{ id: 1, name: '待执行' },{ id: 2, name: '已执行' },{ id: 3, name: '已执行，失败' }]);
        }else {
            //编辑
            mini.get("type").setData([{ id: 0, name: '未启用' },{ id: 1, name: '待执行' }]);
        }
    });

    //拿到省份
    function getProvince(){
        $.ajax({
            url: '/azbj/sjtjfx/xzzttj/getProvince',
            type: "GET",
            success: function (res) {
                for(var i=0;i<res.length;i++){
                    res[i].codeId = res[i].codeId.slice(0,2);
                }
                res.unshift({codeId: '-1', codeName: '通用'});
                mini.get("province").setData(res);
                if(flag===2){
                    mini.get("carryResult").setData(res);
                }
                //判断执行返回框
                if(flag===2 && mini.get("type").getValue()!=0 && mini.get("type").getValue()!=1){
                    //处理未完成的省份
                    var provinceArr = [];//要执行的省份
                    if(mini.get("province").getValue()==-1){
                        for(var i=1;i<res.length;i++){
                            provinceArr[i-1] = res[i].codeId;
                        }
                    }else{
                        provinceArr = mini.get("province").getValue().split(',')
                    }
                    var carryResultArr = mini.get("carryResult").getValue().split(',');//已执行的省份
                    var carryResultString = '';//返现的省份
                    for(var j=0;j<provinceArr.length;j++){
                        if(carryResultArr.indexOf(provinceArr[j])===-1){
                            carryResultString += provinceArr[j] + ','
                        }
                    }
                    mini.get("carryResult").setValue(carryResultString.slice(0,carryResultString.length-1));
                    $('#carryResultId').show();
                }


                //-------------------------
            }
        });
    }

    function click(){
        mini.confirm("该操作将会直接影响到库内容，请谨慎检查、操作！！！",
            "确定？",
            function (action) {
                if (action == "ok") {
                    saveData();
                }
            });
    }

    function saveData(){
        form.validate();
        if (!form.isValid() || !checkSQL()) {
            return;
        }
        var data = form.getData();
        data['flag'] = flag;
        data['sqlType'] = sqlType;
        var json = mini.encode(data);
        $.ajax({
            url: "/azbj/dsqx/addSql",
            data: {data: json},
            type: "post",
            success: function (res) {
                if(res.code===1){
                    mini.alert(res.data);
                    if(flag===1){
                        form.clear();
                    }
                }else {
                    mini.alert(res.data);
                }
            },
            error: function () {
                mini.unmask(document.body);
                mini.alert("系统异常");
            }
        })
    }

    //处理SQL语句
    function checkSQL(){
        var sql = mini.get("content").getValue().toLowerCase();
        if( existCount(sql.substring(0,6)) && mustExistCount(sql) && errorSqlCount(sql) && estimateLine(sql) ){
            return true;
        }
        return false;
    }

    //语句中包含其一
    function existCount(sql){
        var existCount = {
            insert:1,
            delete:2,
            update:3,
            //select:4,
        };
        for(var item in existCount){
            if( sql.indexOf(item) !==-1 ){
                sqlType = existCount[item];
                return true;
            }
        }
        mini.alert('SQL语句不满足三大增、删、改语句');
        return false;
    }

    //语句中必须包含
    function mustExistCount(sql){
        var mustExistCount = ['from','where',';'];
        if(sql.indexOf('insert')!==-1){
            mustExistCount[0] = 'into ';
            mustExistCount[1] = 'values ';
        }else if(sql.indexOf('update ')!==-1){
            mustExistCount[0] = 'set';
        }
        for(var i=0;i<mustExistCount.length;i++){
            if(sql.indexOf(mustExistCount[i])===-1){
                mini.alert('SQL语句不合规，请查看是否缺少\"from、where、set;\"等关键字');
                return false;
            }
        }
        if(sql.slice(-1)!==';'){
            mini.alert('SQL语句不合规，请查看是否缺少\"from、where、set;\"等关键字');
            return false;
        }
        return true;
    }

    //判断语句中不能存在的内容
    function errorSqlCount(sql){
        var errorSqlCount = ['*',' where 1=1','\'\'', '#'];
        for(var i=0;i<errorSqlCount.length;i++){
            if(sql.indexOf(errorSqlCount[i])!==-1){
                mini.alert("语句中不可包含\"*\"等范围性条件，不可包含特殊符号");
                return false;
            }
        }
        return true;
    }

    //可扩展，多条语句判断
    function estimateLine(sql){
        return true;
    }

    //多选省份检查
    function provinceCheck(){
        var province = mini.get("province");
        if(province.getValue().indexOf('-1')!==-1){
            province.setValue('-1');
        }
    }
</script>
</body>
</html>